using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Get, "PsSpecNoun")]
    public class GetPsSpecNounCommand : Cmdlet
    {
        [Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 50)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = "*";

        [Parameter(Mandatory = false, Position = 1, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 2000)]
        public String Definition
        {
            get
            {
                return _definition;
            }
            set
            {
                _definition = value;
                definitionSpecified = true;
            }
        }
        private bool definitionSpecified = false;
        private string _definition = "*";

        [Parameter(Mandatory = false, Position = 2)]
        public String ProjectName
        {
            get
            {
                return _projectName;
            }
            set
            {
                _projectName = value;
                projectNameSpecified = true;
            }
        }
        private bool projectNameSpecified = false;
        private string _projectName = "*";

        [Parameter(Mandatory = false)]
        public SwitchParameter Quiet
        {
            get
            {
                return _quiet;
            }
            set
            {
                _quiet = (bool) value;
            }
        }
        private bool _quiet = false;

        [Parameter(Mandatory = false)]
        public String ServiceUri
        {
            get
            {
                return _serviceUri;
            }
            set
            {
                _serviceUri = value;
            }
        }
        private string _serviceUri = PsSpecCmdletUtils.Constants.ServiceUri;


        protected override void ProcessRecord()
        {
            PsSpecCmdletService.PsSpecNoun[] nouns = null;

            PsSpecCmdletService.CmdletService service =
                new PsSpecCmdletService.CmdletService();
            service.Url = ServiceUri;

            service.UseDefaultCredentials = true;

            string convertedName = PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(Name);
            string convertedDefinition = PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(Definition);
            string convertedProject = PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(ProjectName);
            nouns = service.GetNoun(convertedName, convertedDefinition, convertedProject);

            bool nounFound = false;
            foreach (PsSpecCmdletService.PsSpecNoun noun in nouns)
            {
                nounFound = true;

                // If we're in quiet mode, don't write out the noun
                if (!Quiet)
                {
                    WriteObject(new PsSpecNoun(noun.Id, noun.Name, noun.Definition, noun.ProjectName));
                }
            }

            if (Quiet)
            {
                WriteObject(nounFound);
            }
            else
            {
                bool globbed = ! String.Equals(Name, convertedName, StringComparison.InvariantCultureIgnoreCase);

                if (definitionSpecified)
                {
                    globbed |= ! String.Equals(Definition, convertedDefinition, StringComparison.InvariantCultureIgnoreCase);
                }

                if (projectNameSpecified)
                {
                    globbed |= ! String.Equals(ProjectName, convertedProject, StringComparison.InvariantCultureIgnoreCase);
                }

                // Generate an error if no nouns found, and no globbing was done
                if ((! nounFound) && (! globbed))
                {
                    string message = String.Format(resources.NounNotFound, Name);
                    WriteError(new ErrorRecord(
                        new Exception(message),
                        "NOUN_NOT_FOUND",
                        ErrorCategory.ObjectNotFound,
                        Name));
                }
            }
        }
    }
}
